import request from '@/ulits/request'

export default {
  state: {
    getCarList: [],//购物车的数据
  },
  //简化数据(处理数据)
  getters: {
    cartList(state) {
      return state.getCarList[0] || []
    }
  },
  mutations: {
    GETCARTLIST(state, res) {
      state.getCarList = res
    }
  },
  actions: {
    // cart/cartList  GET请求
    async getCarList(state) {
      const { data: res } = await request.get('/cart/cartList')
      state.commit('GETCARTLIST', res)
    },
    // /cart/deleteCart/{skuId}  delete请求
    async deleteCartListById(state, data) {
      await request.delete(`/cart/deleteCart/${data.skuId}`)
    },
    // /cart/checkCart/{skuID}/{isChecked} get请求 修改商品的勾选状态
    async updateCheckedById(state, data) {
      let res = await request.get(`/cart/checkCart/${data.skuID}/${data.isChecked}`)
    },
    delteAllChecked(state) {
      state.getters.cartList.cartInfoList.forEach(item => {
        item.isChecked == 1 ? state.dispatch('deleteCartListById', { skuId: item.skuId }) : ''
      });
    },
    updateAllCartIschecked(state, isChecked) {
      state.getters.cartList.cartInfoList.forEach((item) => {
        state.dispatch('updateCheckedById', { skuID: item.skuId, isChecked: isChecked })
      })
    }
  }
}